package net.member.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import net.member.db.MemberBean;
import net.member.db.MemberDAO;

public class MemberLoginAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		ActionForward forward = new ActionForward();
		MemberBean memberbean = new MemberBean();
		MemberDAO memberdao = new MemberDAO();
		
		String member_id = request.getParameter("member_id");
		String member_pw = request.getParameter("member_pw");
		
		int usercheck = memberdao.useCheck(member_id , member_pw);
		
		if(usercheck == 1){
			session.setAttribute("member_id", member_id);
			if(memberdao.isAdmin(member_id)){

				forward.setRedirect(false);
				forward.setPath("./GoodsList.ag");
				System.out.println("로그인성공");
				return forward;
			}else{
				forward.setRedirect(true);
				forward.setPath("./GoodsList.go?item=new_item");
				return forward;
			}
		}else if(usercheck == 0){
			response.setContentType("text/html; charset=euc-kr");
			PrintWriter out = response.getWriter();
			out.println("<script>");
			out.println("alert('비밀번호가 일치하지 않습니다.')");
			out.println("history.back");
			out.println("</script>");
			out.close();			
		}else{
			response.setContentType("text/html; charset=euc-kr");
			PrintWriter out = response.getWriter();
			out.println("<script>");
			out.println("alert('아이디가 일치하지 않습니다.')");
			out.println("history.back");
			out.println("</script>");
			out.close();		
		}
		return null;
	}

}
